//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class CipherOutputStream extends FilterOutputStream {
    private javax.vpncrypto.Cipher cipher;
    private OutputStream output;
    private byte[] ibuffer = new byte[1];
    private byte[] obuffer;
    private boolean closed = false;

    public CipherOutputStream(OutputStream var1, Cipher var2) {
        super(var1);
        this.output = var1;
        this.cipher = var2;
    }

    protected CipherOutputStream(OutputStream var1) {
        super(var1);
        this.output = var1;
        this.cipher = new NullCipher();
    }

    public void write(int var1) throws IOException {
        this.ibuffer[0] = (byte)var1;
        this.obuffer = this.cipher.update(this.ibuffer, 0, 1);
        if (this.obuffer != null) {
            this.output.write(this.obuffer);
            this.obuffer = null;
        }

    }

    public void write(byte[] var1) throws IOException {
        this.write(var1, 0, var1.length);
    }

    public void write(byte[] var1, int var2, int var3) throws IOException {
        this.obuffer = this.cipher.update(var1, var2, var3);
        if (this.obuffer != null) {
            this.output.write(this.obuffer);
            this.obuffer = null;
        }

    }

    public void flush() throws IOException {
        if (this.obuffer != null) {
            this.output.write(this.obuffer);
            this.obuffer = null;
        }

        this.output.flush();
    }

    public void close() throws IOException {
        if (!this.closed) {
            this.closed = true;

            try {
                this.obuffer = this.cipher.doFinal();
            } catch (BadPaddingException | IllegalBlockSizeException var3) {
                this.obuffer = null;
            }

            try {
                this.flush();
            } catch (IOException var2) {
            }

            this.out.close();
        }
    }
}
